<?php

namespace app\admin\controller\service;

use app\common\controller\Backend;
use app\common\model\ServiceImage;
use app\common\model\ServiceMainBusiness;
use app\common\model\ServiceShop;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use think\exception\ValidateException;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Shop extends Backend
{

    /**
     * Shop模型对象
     * @var \app\admin\model\service\Shop
     */
    protected $model = null;
    protected $searchFields = 'member.member_name,name,address,phone,main_business';

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\ServiceShop;

    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                ->with(['member','shop','province','city','districts','service'])
                ->where($where)
                ->where('service_shop.is_del',0)
                ->order($sort, $order)
                ->paginate($limit)->each(function ($item){
                    $item['main_business'] = ServiceMainBusiness::where(['id'=>['in',$item['main_business']]])->column('name');
                    return $item;
                });

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }


    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $row['images'] = ServiceImage::where(['main_id'=>$row['id'],'service_type'=>$row['service_type']])->select()->column('image');
        if (!empty($row['images'] )){
            $row['images'] = implode(',',$row['images']);
        }
//        halt($row);
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }


    /**
     * 审核通过
     */
    public function pass($ids = NULL)
    {
        try{
            if (empty($ids)){
                throw new Exception('参数ID缺失');
            }
            $info = ServiceShop::get($ids);
            if (empty($info)){
                throw new Exception('该条记录信息不存在');
            }
            $flag = ServiceShop::update(['status'=>1,'show'=>1],['id'=>$ids]);
            if ($flag == false){
                throw new Exception('更新失败');
            }
            $this->success();
        }catch (Exception $e){
            $this->error($e->getMessage());
        }
    }

    /**
     * 关闭
     */
    public function down($ids = NULL)
    {
        try{
            if (empty($ids)){
                throw new Exception('参数ID缺失');
            }
            $info = ServiceShop::get($ids);
            if (empty($info)){
                throw new Exception('该条记录信息不存在');
            }
            $flag = ServiceShop::update(['status'=>2,'show'=>2],['id'=>$ids]);
            if ($flag == false){
                throw new Exception('更新失败');
            }
            $this->success();
        }catch (Exception $e){
            $this->error($e->getMessage());
        }
    }



}
